package de.koller.reason.gui.component.mo;

import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.reason.gui.component.RComponent;
import de.koller.reason.gui.textureObj.IPart;
import de.koller.reason.gui.textureObj.SubPart;
import de.koller.reason.gui.textureObj.TextureObject;
import java.util.HashSet;
import java.util.Set;
import org.lwjgl.opengl.GL11;

public class Glower {

	public static final int GLOW_STRENGTH = 255;
	private final Set<RComponent> glows = new HashSet<>();
	private TextureObject glowTexture;
	
	private final IPart[] parts = new IPart[] {
		new SubPart(  0,  0, 40, 40 ),
		new SubPart( 40,  0, 40, 40 ),
		new SubPart( 80,  0, 40, 40 ),
		new SubPart( 80, 40, 40, 40 ),
		new SubPart( 80, 80, 40, 40 ),
		new SubPart( 40, 80, 40, 40 ),
		new SubPart(  0, 80, 40, 40 ),
		new SubPart(  0, 40, 40, 40 )
	};
	
	public void create() {
		ParticleTexture.MAG_FILTER = GL11.GL_LINEAR;
		ParticleTexture.MIN_FILTER = GL11.GL_LINEAR;
		ParticleTexture t = ParticleTexture.generate( 128, 128 );
		
		for( int x = 0; x < 40; x++ )
			for( int y = 0; y < 40; y++ ) {
				int v = GLOW_STRENGTH / 40 * y; 
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), x+40, y );
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), x+40, 119-y );
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), y, x+40 );
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), 119-y, x+40 );
				
				v = (int)(GLOW_STRENGTH / 40 * (40-( Math.sqrt( (40-x)*(40-x) + (40-y)*(40-y) ) )));
				if( v < 0 ) v = 0;
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), x, y );
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), 119-x, y );
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), 119-x, 119-y );
				t.setPixel( ColorIntUtil.getIntColor( v, 0xff, 0xff, 0xff ), x, 119-y );
			}
		
		t.flush();
		
		glowTexture = TextureObject.create( t );
	}
	
	public void renderGlows() {
		glowTexture.bind();
		
		GL11.glEnable( GL11.GL_BLEND );
		GL11.glBlendFunc( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA );
		
		for( RComponent c : glows ) {
			int gc = c.getColorGlow();
			GL11.glColor3f( ColorIntUtil.getRValueF( gc ), ColorIntUtil.getGValueF( gc ), ColorIntUtil.getBValueF( gc ) );
			float gd = c.getGlowRange();
			
			glowTexture.draw( c.getX()-gd, c.getY()-gd, gd, gd, c.getZ()+1, parts[0] );
			glowTexture.draw( c.getX(), c.getY()-gd, c.getWidth(), gd, c.getZ()+1, parts[1] );
			glowTexture.draw( c.getX() + c.getWidth(), c.getY()-gd, gd, gd, c.getZ()+1, parts[2] );
			glowTexture.draw( c.getX() + c.getWidth(), c.getY(), gd, c.getHeight(), c.getZ()+1, parts[3] );
			glowTexture.draw( c.getX() + c.getWidth(), c.getY() + c.getHeight(), gd, gd, c.getZ()+1, parts[4] );
			glowTexture.draw( c.getX(), c.getY()+c.getHeight(), c.getWidth(), gd, c.getZ()+1, parts[5] );
			glowTexture.draw( c.getX()-gd, c.getY()+c.getHeight(), gd, gd, c.getZ()+1, parts[6] );
			glowTexture.draw( c.getX()-gd, c.getY(), gd, c.getHeight(), c.getZ()+1, parts[7] );
		}
		
		GL11.glDisable( GL11.GL_BLEND );
	}
	
	public void letGlow( RComponent comp ) {
		if( !glows.contains( comp ) )
			glows.add( comp );
	}
	
	public void stopGlow( RComponent comp ) {
		glows.remove( comp );
	}
	
}
